if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLastPatientCareChargeFeedDate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLastPatientCareChargeFeedDate]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


/***************************************************************************
* Name:			GetLastPatientCareChargeFeedDate 'MGH',0, '20080501'
* Purpose:		Return the last feed date loaded for daily or monthly Patient
*				Care Charges for a given institution.
*
* PARAMETERS
* Name				Description					
* -------------		-----------------------------------------------------------
* n/a
*
* ERRORS USED		Description
* -------------		-----------------------------------------------------
* n/a
*
* RETURN VALUE
* Value    			Description					
* --------------	------------------------------------------------------
* @_exitStatus		An exit status of either true or false
***************************************************************************/
CREATE PROCEDURE [dbo].[GetLastPatientCareChargeFeedDate] 
( 
	@Institution varchar(50)
	,@IsDaily bit
	,@maxFeedDateString varchar(8) OUTPUT
)
AS 
BEGIN

	SET NOCOUNT ON

	declare @FeedDate datetime

	/*
	>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	D E P E N D I N G  O N  D A I L Y  O R  M O N T H L Y  A N D  I N S T I T U T I O N
	Determine the last feed date.
	>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	*/
	select distinct @FeedDate = max(c.feedDate) from PatientCareExpenseDetail c
	inner join agragreement_fct aaf on aaf.fundid = c.agrfundid
	where aaf.institution = @Institution --and c.IsDaily = @IsDaily

	-- Default the feed date if null.
	If @FeedDate is null set @FeedDate = '1/1/1900'

	-- Format the feed date string.
	set @maxFeedDateString = cast(year(@FeedDate) as varchar) 
							 +	case when len(cast(month(@FeedDate) as varchar)) < 2 then 
									'0'+ cast(month(@FeedDate) as varchar) 
								else cast(month(@FeedDate) as varchar) 
								end
							 +	case when len(cast(day(@FeedDate) as varchar)) < 2 then 
									'0'+ cast(day(@FeedDate) as varchar) 
								else cast(day(@FeedDate) as varchar) 
								end

END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO